﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_MultiImageUploadField:PageForm_SingleInputFieldBase
    {
        private SelectorItem[] _lst = null;
        //private string _name = "";
        private bool _disabled = false;
        private string _uploadUrl = "";
        private int _fileSize = 0;
        private int _maxCount = -1;

        public PageForm_MultiImageUploadField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_multiimageuploadfield multiimageuploadfield");
        }

        //public PageForm_MultiImageUploadField Name(string name)
        //{
        //    _name = name;

        //    return this;
        //}

        public PageForm_MultiImageUploadField Images(IEnumerable<string> imageUrls)
        {
            _lst = imageUrls?.Select(x=>new SelectorItem("",x)).ToArrayEx();

            return this;
        }

        public PageForm_MultiImageUploadField Images(IEnumerable<SelectorItem> images)
        {
            _lst = images.ToArrayEx();

            return this;
        }

        public PageForm_MultiImageUploadField Images<TData>(IEnumerable<TData> lst,Func<TData, string> srcGetter, Func<TData, bool> disableGetter = null)
        {
            _lst = lst.Select(x => new SelectorItem("", srcGetter(x), disableGetter?.Invoke(x) ?? false)).ToArrayEx();

            return this;
        }

        /// <summary>
        /// 可上传的最大大小,单位为KB
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_MultiImageUploadField MaxFileSize(int size)
        {
            _fileSize = size * 1024;
            this.Attributes.Data("maxfilesize", _fileSize.ToStringEx());
            return this;
        }

        public PageForm_MultiImageUploadField LimitFileCount(int maxCount)
        {
            _maxCount = maxCount;

            this.Attributes.Data("maxcount", _maxCount.ToStringEx());

            return this;
        }


        public PageForm_MultiImageUploadField UploadUrl(string url)
        {
            _uploadUrl = url;

            return this;
        }

        public PageForm_MultiImageUploadField Disabled(bool disabled=true)
        {
            _disabled = disabled;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            this.Data("name", _name);
            this.Data("uploadurl", _uploadUrl)
                .Data("maxfilesize", _fileSize.ToStringEx())
                .Data("disabled", _disabled.ToStringEx())
                ;

            base.BeforeRender(writer);
        }

        protected override void RenderContent(TextWriter writer)
        {
            //writer.WriteLine("<button type=\"button\" class=\"btn btn-success addfile\" >上传文件</button>");

            writer.WriteLine("<table class=\"table table-bordered table-striped\" lay-ignore webui-ignore  style=\"display: table;margin-bottom: 0px;\">");
            writer.Write("<thead><tr><th width=\"50%\">图片</th>");
            
            if (!_disabled )
            {
                writer.WriteLine("<th width=\"87px\">上传进度</th><th width=\"25%\">");

                if (_maxCount <= 0)
                {
                    writer.WriteLine($"<button type=\"button\" class=\"btn btn-success btn-xs addfile\" {_disabled.If("disabled")}>选择图片</button>");
                }
            }

            writer.WriteLine("</th></tr></thead>");

            writer.WriteLine("<tbody>");

            //if (_lst.HasData())
            //{
                if (_maxCount>0)
                {
                    for (int i = 0; i < _maxCount; i++)
                    {
                        var item = (_lst.HasData() && i < (_lst?.Length??0)) ? _lst[i] : null;

                        writer.WriteLine("<tr>");

                        writer.WriteLine($"<td class='filename'><img data-src=\"{item?.Value}\" /><input type=\"hidden\" name=\"{_name}\" value=\"{item?.Value}\" /></td>");

                        
                        if (!(item?.Disabled??false) && !_disabled)
                        {
                            writer.WriteLine("<td style=\"vertical-align: middle;\"></td><td style=\"vertical-align: middle;\">");

                            writer.Write("<span class=\"badge bg-green badge-view\" style=\"cursor: pointer;\">查看图片</span><br/>");

                            writer.Write("<span class=\"badge bg-red badge-edit\" style=\"cursor: pointer;\">修改</span><br/>");

                            if (_maxCount<=0)
                            {
                                writer.Write("<a class=\"badge bg-red badge-del\" style=\"cursor: pointer;\">删除</a>");
                            }
                            else
                            {
                                writer.Write("<a class=\"badge bg-red badge-clear\" style=\"cursor: pointer;\">清除</a>");
                            }
                            

                            writer.Write("</td>");
                        }

                       

                        writer.WriteLine("</tr>");
                    }
                }
                else
                {
                    if (_lst.HasData())
                    {
                        foreach (var item in _lst)
                        {
                            writer.WriteLine("<tr>");

                            writer.WriteLine($"<td class='filename'><img data-src=\"{item.Value}\" /><input type=\"hidden\" name=\"{_name}\" value=\"{item.Value}\" /></td>");


                            if (!item.Disabled && !_disabled)
                            {
                                writer.WriteLine("<td style=\"vertical-align: middle;\"></td><td style=\"vertical-align: middle;\">");

                                writer.Write("<span class=\"badge bg-green badge-view\" style=\"cursor: pointer;\">查看图片</span><br/>");

                                writer.Write("<span class=\"badge bg-red badge-edit\" style=\"cursor: pointer;\">修改</span><br/>");


                                writer.Write("<a class=\"badge bg-red badge-clear\" style=\"cursor: pointer;\">清除</a>");

                                writer.Write("</td>");
                            }



                            writer.WriteLine("</tr>");
                        }
                    }

                }
                             
            //}


            writer.WriteLine("</tbody>");
            writer.WriteLine("</table>");
        }
    }
}
